<?php
ini_set('default_charset', 'utf-8');
//header("Content-type: text/ xml");
/**
 * @file
 * User has successfully authenticated with Twitter. Access tokens saved to session and DB.
 */

/* Load required lib files. */
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');

function getConnectionWithAccessToken($oauth_token, $oauth_token_secret) {
  $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth_token, $oauth_token_secret);
  return $connection;
}

//$connection = getConnectionWithAccessToken($Access_token, $Access_token_secret);
$connection = getConnectionWithAccessToken("44202617-FA47K0Au1S7XDKLshzHbXd37GCIcylVvJ83yEuuI", "vwG1X0doMm1Jm77EgXXHDanDXyQCKDHnw2guH5r200");

/* If method is set change API call made. Test is called by default. */
//$content = $connection->get('account/verify_credentials');
$aaaaa = $connection->get("https://api.twitter.com/1/account/verify_credentials.json");
var_dump($aaaaa);

$query = $connection->get("https://api.twitter.com/1/statuses/user_timeline.json?count=20");
//print_r("status");

foreach ($query as $q)
{
	$id_str = $q->id_str;
	
	$query_2 = $connection->get("https://api.twitter.com/1/statuses/show.json?id=$id_str&include_entities=1&include_rts=1");
	//var_dump($query_2);
	$entities = $query_2->entities;
	
	$text = $q->text;
	$screen_name = $q->user->screen_name;
	$user = $q->user->name;
	$created_at = $q->created_at;
	
	$Split_text = strstr( $text, "http" );
	$media_url = substr($Split_text,7);
	$Description_text = substr($text,0,strpos($text, 'http'));
	$WebLink = "twitter.com/$screen_name/status/$id_str";
	
	echo '<Media ID="'.$id_str.'" MediaType="" CommentCount="'.count($query_2->in_reply_to_status_id_str).'" WebLink="'.$WebLink.'" Source="Twitter" Author="'.$user.'" lastUpdate="'.$created_at.'" >';
	
	//------------------------Media-text Info------------------------//
	echo '<Text ID="'.$id_str.'" Title="" Description="'.$Description_text.'" />';
	//var_dump($text);
	//------------------------Media-image Info------------------------//
	if(!empty($entities->media))
	{
		$image_url = substr($query_2->entities->media[0]->media_url,7);
		echo '<Image ID="'.$id_str.'" Type="URL" FileName="'.$image_url.'" Description="" />';
		
	}
	
	//------------------------Media-video Info------------------------//
	if(!empty($entities->urls))
	{
		$video_url = $query_2->entities->urls[0]->display_url;
		echo '<Video ID="'.$id_str.'" Type="URL" FileName="'.$video_url.'" Description="" />';
	}
	
	echo '</Media>';
	//var_dump($entities);
	//print_r($q->id_str."</br>");
}
//var_dump($query);